//
//  CategoryTableViewCell.swift
//  FM
//
//  Created by x on 2017/9/6.
//  Copyright © 2017年 HLB. All rights reserved.
//

import UIKit

class CategoryTableViewCell: UITableViewCell {

    var model:HomeFM? {
        didSet {
            iconImageView.sd_setImage(with: model?.iconImageURL)
            titlelLable.text = model?.title
            speakLable.text = model?.speak
            lisetnLable.text = "收听量 "+(model?.viewnum)!
        }
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setupUI()
    }
    
    /// 初始化UI
    func setupUI() -> Void {
        
        //1.添加子控件
        contentView.addSubview(iconImageView)
        contentView.addSubview(titlelLable)
        contentView.addSubview(speakLable)
        contentView.addSubview(lisetnLable)
        
        //2.布局子控件
        var cons:[NSLayoutConstraint] = [NSLayoutConstraint]()
        let tt = ["iconImageView":iconImageView,"titlelLable":titlelLable,"speakLable":speakLable,"lisetnLable":lisetnLable] as [String:Any]
        
        cons += NSLayoutConstraint.constraints(withVisualFormat: "H:|-5-[iconImageView(60)]", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: nil, views: tt)
        cons += NSLayoutConstraint.constraints(withVisualFormat: "V:|-10-[iconImageView(60)]", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: nil, views: tt)
        
        cons += NSLayoutConstraint.constraints(withVisualFormat: "H:[iconImageView]-5-[titlelLable]", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: nil, views: tt)
        cons += NSLayoutConstraint.constraints(withVisualFormat: "V:|-10-[titlelLable]", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: nil, views: tt)
        
        cons += NSLayoutConstraint.constraints(withVisualFormat: "H:[iconImageView]-5-[speakLable]", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: nil, views: tt)
        cons += NSLayoutConstraint.constraints(withVisualFormat: "V:[titlelLable]-10-[speakLable]", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: nil, views: tt)
        
        cons += NSLayoutConstraint.constraints(withVisualFormat: "H:[iconImageView]-5-[lisetnLable]", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: nil, views: tt)
        cons += NSLayoutConstraint.constraints(withVisualFormat: "V:[speakLable]-8-[lisetnLable]", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: nil, views: tt)
        
        contentView.addConstraints(cons)
        
    }
    
    func rowHeight(model:HomeFM) -> CGFloat {
    
        self.model = model
        
        self.layoutIfNeeded()
        
        return iconImageView.frame.maxY
    }
    
    //MARK: -- 懒加载
    
    private lazy var iconImageView:UIImageView = UIImageView.layoutImageView()
    
    private lazy var titlelLable:UILabel = UILabel.lableWith(title: "", titleColor: UIColor.black, fontSize: 14)
    
    private lazy var speakLable:UILabel = UILabel.lableWith(title: "", titleColor: UIColor(white: 0.8, alpha: 1.0), fontSize: 10)
    
    private lazy var lisetnLable:UILabel = UILabel.lableWith(title: "", titleColor: UIColor(white: 0.8, alpha: 1.0), fontSize: 10)
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
